﻿using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Parallel.Translate
{
    class Program
    {
        static async Task Main(string[] args)
        {

            if (args.Length > 0)
            {
                await ProcessInput(args.First());
                return;
            }
            var name = Path.GetFileNameWithoutExtension(Environment.CommandLine);

            while (true)
            {
                try
                {
                    Console.WriteLine($" {name}: 请指定xml注释文档文件或目录地址.");
                    var line = Console.ReadLine();
                    await ProcessInput(line);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        static async Task ProcessInput(string path)
        {
            if (string.IsNullOrEmpty(path)) return;
            path = path.Trim('"');
            if (File.Exists(path))
            {
                await TranslateAsync(path);
            }
            else if (Directory.Exists(path))
            {
                foreach (var file in Directory.GetFiles(path, "*.xml"))
                {
                    await TranslateAsync(file);
                }
            }
            else
            {
                Console.WriteLine($"{path}不是文件也不是目录");
            }
        }

        static async Task TranslateAsync(string file)
        {
            var xml = new XmlTree(new FileInfo(file), new BaiduTranslate());
            await xml.TranslateAsync();
            xml.Save();
            Console.WriteLine($"{file}处理完毕");
        }
    }
}
